package org.example.database.controller;

import org.example.database.entity.RecruitmentPosition;
import org.example.database.result.Result;
import org.example.database.service.RecruitmentPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 招聘职位管理控制器
 * 提供对招聘职位信息的增删改查接口
 */
@RestController
@RequestMapping("/recruitmentPosition")
public class RecruitmentPositionController {

    @Autowired
    private RecruitmentPositionService recruitmentPositionService;

    /**
     * 添加新的招聘职位
     *
     * @param position 职位信息实体，包含职位名称、部门、招聘人数等字段
     * @return 操作结果，成功时返回插入的记录数
     */
    @PostMapping("/insert")
    @CacheEvict (value = "recruitmentPositionCache",key = "#position.jobNumber")
    public Result<?> insert(@RequestBody RecruitmentPosition position) {
        try {
            int result = recruitmentPositionService.insert(position);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据职位编号查询职位详情
     *
     * @param jobNumber 职位编号，唯一标识一个职位
     * @return 操作结果，成功时返回职位详细信息
     */
    @GetMapping("/selectById/{jobNumber}")
    @Cacheable (value = "recruitmentPositionCache",key = "#jobNumber")
    public Result<?> selectById(@PathVariable Integer jobNumber) {
        try {
            RecruitmentPosition position = recruitmentPositionService.selectById(jobNumber);
            return Result.success(position);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询所有招聘职位信息
     *
     * @return 操作结果，成功时返回所有职位的列表
     */
    @GetMapping("/selectAll")
    @Cacheable (value = "recruitmentPositionCache",key = "all")
    public Result<?> selectAll() {
        try {
            List<RecruitmentPosition> positions = recruitmentPositionService.selectAll();
            return Result.success(positions);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据学历要求筛选职位
     *
     * @param educationalRequirement 学历要求，如"本科"、"硕士"等
     * @return 操作结果，成功时返回符合条件的职位列表
     */
    @GetMapping("/selectByEducationalRequirement/{educationalRequirement}")
    @Cacheable (value = "recruitmentPositionCache",key = "#educationalRequirement")
    public Result<?> selectByEducationalRequirement(@PathVariable String educationalRequirement) {
        try {
            List<RecruitmentPosition> positions = recruitmentPositionService.selectByEducationalRequirement(educationalRequirement);
            return Result.success(positions);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新招聘职位信息
     *
     * @param position 职位信息实体，必须包含要更新的职位编号
     * @return 操作结果，成功时返回更新的记录数
     */
    @PutMapping("/update")
     @CacheEvict (value = "recruitmentPositionCache",key = "#position.jobNumber")
    public Result<?> update(@RequestBody RecruitmentPosition position) {
        try {
            int result = recruitmentPositionService.update(position);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据职位编号删除职位信息
     *
     * @param jobNumber 要删除的职位编号
     * @return 操作结果，成功时返回删除的记录数
     */
    @DeleteMapping("/deleteById/{jobNumber}")
    @CacheEvict (value = "recruitmentPositionCache",key = "#jobNumber")
    public Result<?> deleteById(@PathVariable Integer jobNumber) {
        try {
            int result = recruitmentPositionService.deleteById(jobNumber);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}